package com.zenithmind.news.config;

import com.zenithmind.common.config.AuthInterceptorConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * 新闻服务认证配置 - 遵循单一职责原则
 * 专门负责认证拦截器的配置
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@Configuration
public class NewsAuthConfig {

    /**
     * 配置认证拦截器的跳过路径
     * 使用统一的端点配置，确保一致性
     */
    @Bean
    public BeanPostProcessor newsAuthPropertiesPostProcessor() {
        return new BeanPostProcessor() {
            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof AuthInterceptorConfig.AuthProperties && "authProperties".equals(beanName)) {
                    AuthInterceptorConfig.AuthProperties properties = (AuthInterceptorConfig.AuthProperties) bean;
                    String[] publicEndpoints = NewsEndpointConfig.getAllPublicEndpoints();
                    properties.setSkipPaths(Arrays.asList(publicEndpoints));
                    log.info("已为新闻服务认证拦截器设置跳过路径配置，路径数量: {}", publicEndpoints.length);
                }
                return bean;
            }
        };
    }
}
